import 'dart:io';

import 'package:center_beijing/generated/l10n.dart';
import 'package:center_beijing/login_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:orientation/orientation.dart';

import 'common/ChineseCupertinoLocalizations.dart';
import 'common/styles.dart';
import 'config/global_config.dart';
import 'config/routeConfig.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
void main() {
  runApp(MyHomePage());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 812),

      builder: () => GetMaterialApp(
        initialRoute: RouteConfig.loginPage,
        debugShowCheckedModeBanner: true,
        getPages: RouteConfig.getPages,
        title: '中交御制',
        theme: ThemeData(
            scaffoldBackgroundColor: Styles.MainBackgroundColor
        ),
        localizationsDelegates: [
          S.delegate,
          //此处
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          ChineseCupertinoLocalizations.delegate,
          // const FallbackCupertinoLocalisationsDelegate(),

        ],
        supportedLocales: [
          //此处
          const Locale('zh', 'CN'),
        ],
        builder: EasyLoading.init(builder: (context, widget) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget,
          );
        }),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, @required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  final JPush jpush = new JPush();

  @override
  void initState() {
    super.initState();
    OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
            print("flutter onReceiveNotification: $message");

          }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");

      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");

      }, onReceiveNotificationAuthorization:
          (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");

      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    jpush.setup(
      appKey: "4ccd54b119c8a0187b0cd30b", //你自己应用的 AppKey
      channel: "theChannel",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));

    // Platform messages may fail, so we use a try/catch PlatformException.
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
      // Fluttertoast.showToast(msg: "$rid");
      GlobalConfig.registerId = rid;
    });

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    jpush.setBadge(0);
  }
  List<Locale> an = [
    const Locale('zh', 'CH'),
    const Locale('en', 'US'),
  ];
  List<Locale> ios = [
    const Locale('en', 'US'),
    const Locale('zh', 'CH'),
  ];
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 812),

      builder: () => GetMaterialApp(
        initialRoute: RouteConfig.loginPage,
        debugShowCheckedModeBanner: true,
        getPages: RouteConfig.getPages,
        title: '中交御制',
        theme: ThemeData(
            scaffoldBackgroundColor: Styles.MainBackgroundColor
        ),
        localizationsDelegates: [
          S.delegate,
          //此处
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          ChineseCupertinoLocalizations.delegate,
          // const FallbackCupertinoLocalisationsDelegate(),

        ],
        supportedLocales: [
          //此处
          const Locale('zh', 'CN'),
        ],
        builder: EasyLoading.init(builder: (context, widget) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget,
          );
        }),
      ),
    );
  }
}
class FallbackCupertinoLocalisationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const FallbackCupertinoLocalisationsDelegate();

  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      DefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}